package com.hnkywl.sts.dao.cwgl.kmys;

import com.hnkywl.sts.entity.cwgl.Kmdm;
import com.hnkywl.sts.entity.cwgl.kmys.Tyfzhsmc;
import com.hnkywl.sts.entity.cwgl.kmys.Tyfzhsys;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-23
 * Time: 下午1:37
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class TyfzhsysDao extends SimpleHibernateTemplate<Tyfzhsys, Long> {


    /**
     * 查询通用辅助核算映射
     *
     * @param mc       名称
     * @param code     代码
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Page<Tyfzhsys> findAllTyfzhsys(Long tyfzhsmcId, String mc, String code, int pageNo, int pageSize) {
        Page<Tyfzhsys> page = new Page<Tyfzhsys>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setOrder("asc");
        page.setOrderBy("code");
        if (org.hibernate.tool.hbm2x.StringUtils.isNotEmpty(mc)) {
            return this.findByCriteria(page, new Criterion[]{Restrictions.like("mc", mc, MatchMode.START)});
        }
        if (tyfzhsmcId != null) {
            return this.findByCriteria(page, new Criterion[]{Restrictions.eq("tyfzhsmc.id", tyfzhsmcId)});
        }
        if (org.hibernate.tool.hbm2x.StringUtils.isNotEmpty(code)) {
            return this.findByCriteria(page, new Criterion[]{Restrictions.like("code", code, MatchMode.START)});
        } else
            return this.findAll(page);
    }

    public List<Tyfzhsys> findAllRootByMcId(Long id) {
        String hql = "from Tyfzhsys where tyfzhsmc.id = ? and sjtyfzhsys is null and deleted = 0 ";
        return find(hql, id);
    }


}
